package Presentacion.academico;

import global.Dialogos;
import global.Iconos;

public final class dlgMatriculacion extends javax.swing.JDialog {

    private Iconos ico = new Iconos();

    /**
     * Creates new form dlgMatriculacion
     *
     * @param parent
     * @param modal
     */
    public dlgMatriculacion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setResizable(false);
        setTitle("REGISTRO DE MATRICULA");
        setLocationRelativeTo(this);
        iconos();
        limpiarMatricula();
        visiblidad(true);
    }
    
    private void iconos() {
        btnnuevo.setIcon(ico.getNuevo());
        btnguardar.setIcon(ico.getGuardar());
        btnsalir.setIcon(ico.getSalir());
        btnactualizar.setIcon(ico.getActualizar());
    }

    public void visiblidad(boolean visibilidad) {
        if (visibilidad != true) {
            btnactualizar.setVisible(true);
            btnguardar.setVisible(false);
        } else {
            btnactualizar.setVisible(false);
            btnguardar.setVisible(true);
        }
    }

    private boolean Valadicaion() {
        if ("--Seleccione--".equals(cbotipomatricula.getSelectedItem())) {
            Dialogos.FaltaCampo("Tipo Matricula");
            cbotipomatricula.requestFocus();
            return false;
        } else if ("".equals(txtcedulamatricula.getText())) {
            global.Mensajes.mostrarMensaje("Advertencia!", "Seleccione al alumno", 2);
            cboCurso.requestFocus();
            return false;
        } else if ("--Seleccione--".equals(cboCurso.getSelectedItem())) {
            Dialogos.FaltaCampo("Curso");
            cboCurso.requestFocus();
            return false;
        }
        return true;
    }

    private void limpiarMatricula() {
        btnbuscar.setVisible(true);
        txtmatricula.setText("");
        txtcedulamatricula.setText("");
        txtalumnomatricula.setText("");
        txtdireccionmatricula.setText("");
        cbotipomatricula.setSelectedIndex(0);
        btnguardar.setEnabled(true);
        btnactualizar.setEnabled(true);
        txtmatricula.setText(Integer.toString(frmConsultaMatricula.objMatricula.numeromatricula()));
        frmConsultaMatricula.objMatricula.curso.listarTipoCursos(cboCurso, true);
    }

    public void llenardatosMatricula() {
        btnbuscar.setVisible(false);
        txtcedulamatricula.setText(frmConsultaMatricula.objMatricula.getCedulaRuc());
        txtmatricula.setText(Integer.toString(frmConsultaMatricula.objMatricula.getNumero()));
        cbotipomatricula.setSelectedItem(frmConsultaMatricula.objMatricula.getTipo());
        cboCurso.setSelectedItem(frmConsultaMatricula.objMatricula.curso.getCurso() + " " + frmConsultaMatricula.objMatricula.curso.getParalelo());
        txtalumnomatricula.setText(frmConsultaMatricula.objMatricula.getApellido() + " " + frmConsultaMatricula.objMatricula.getNombre());
        txtdireccionmatricula.setText(frmConsultaMatricula.objMatricula.getDireccion());
    }

    private void set() {
        frmConsultaMatricula.objMatricula.setCedulaRuc(txtcedulamatricula.getText());
        frmConsultaMatricula.objMatricula.setTipo(cbotipomatricula.getSelectedItem().toString());
        frmConsultaMatricula.objMatricula.curso.sacarIdCursoAsignado(cboCurso.getSelectedItem().toString());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar4 = new javax.swing.JToolBar();
        btnnuevo = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnactualizar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();
        PanelMatricula = new javax.swing.JPanel();
        txtcedulamatricula = new javax.swing.JTextField();
        txtalumnomatricula = new javax.swing.JTextField();
        txtdireccionmatricula = new javax.swing.JTextField();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        jLabel33 = new javax.swing.JLabel();
        jLabel34 = new javax.swing.JLabel();
        txtmatricula = new javax.swing.JTextField();
        cbotipomatricula = new javax.swing.JComboBox();
        jLabel35 = new javax.swing.JLabel();
        jLabel36 = new javax.swing.JLabel();
        cboCurso = new javax.swing.JComboBox();
        btnbuscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jToolBar4.setBorder(null);
        jToolBar4.setFloatable(false);
        jToolBar4.setBorderPainted(false);

        btnnuevo.setText("Nuevo");
        btnnuevo.setFocusable(false);
        btnnuevo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnnuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });
        jToolBar4.add(btnnuevo);

        btnguardar.setText("Guardar");
        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnguardar);

        btnactualizar.setText("Actualizar");
        btnactualizar.setFocusable(false);
        btnactualizar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnactualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnactualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnactualizar);

        btnsalir.setText("Salir");
        btnsalir.setFocusable(false);
        btnsalir.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnsalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jToolBar4.add(btnsalir);

        PanelMatricula.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtcedulamatricula.setEditable(false);

        txtalumnomatricula.setEditable(false);

        txtdireccionmatricula.setEditable(false);

        jLabel30.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel30.setText("DIRECCIÓN");

        jLabel31.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel31.setText("ALUMNO");

        jLabel33.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel33.setText("CÉDULA");

        jLabel34.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel34.setText("MATRICULA");

        txtmatricula.setEditable(false);

        cbotipomatricula.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "ORDINARIA", "EXTRAORDINARIA" }));

        jLabel35.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel35.setText("TIPO MATRICULA");

        jLabel36.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel36.setText("CURSO");

        cboCurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnbuscar.setBackground(new java.awt.Color(255, 255, 255));
        btnbuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/buscar.png"))); // NOI18N
        btnbuscar.setFocusable(false);
        btnbuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnbuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PanelMatriculaLayout = new javax.swing.GroupLayout(PanelMatricula);
        PanelMatricula.setLayout(PanelMatriculaLayout);
        PanelMatriculaLayout.setHorizontalGroup(
            PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelMatriculaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelMatriculaLayout.createSequentialGroup()
                        .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel31)
                            .addComponent(jLabel30)
                            .addComponent(jLabel36)
                            .addComponent(jLabel34))
                        .addGap(11, 11, 11)
                        .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtdireccionmatricula)
                            .addComponent(txtalumnomatricula)
                            .addGroup(PanelMatriculaLayout.createSequentialGroup()
                                .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtcedulamatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtmatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                                .addComponent(jLabel35)
                                .addGap(18, 18, 18)
                                .addComponent(cbotipomatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(PanelMatriculaLayout.createSequentialGroup()
                                .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(PanelMatriculaLayout.createSequentialGroup()
                        .addComponent(jLabel33)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        PanelMatriculaLayout.setVerticalGroup(
            PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelMatriculaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(PanelMatriculaLayout.createSequentialGroup()
                        .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtmatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel34)
                            .addComponent(cbotipomatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel35))
                        .addGap(25, 25, 25)
                        .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel33)
                            .addComponent(txtcedulamatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel31)
                    .addComponent(txtalumnomatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel30)
                    .addComponent(txtdireccionmatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(PanelMatriculaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel36)
                    .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(PanelMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PanelMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        this.limpiarMatricula();
    }//GEN-LAST:event_btnnuevoActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        if (Valadicaion() == true) {
            set();
            if (frmConsultaMatricula.objMatricula.ExistenciaMatricula(
                    txtalumnomatricula.getText()) == true) {
                Dialogos.Error("El alumno ya se encuentra matriculado en el periodo actual...");
            } else if (frmConsultaMatricula.objMatricula.guardarMatricula() == true
                    && frmConsultaMatricula.objMatricula.guardarMatriculaCurso() == true) {
                Dialogos.GuardadoconExito();
                frmConsultaMatricula.generartabla();
                limpiarMatricula();
            } else {
                Dialogos.Error("Error en la matriculacion");
            }
        }
    }//GEN-LAST:event_btnguardarActionPerformed

    public void Actulizar() {
        if (Valadicaion() == true) {
            frmConsultaMatricula.objMatricula.setTipo(cbotipomatricula.getSelectedItem().toString());
            frmConsultaMatricula.objMatricula.curso.sacarIdCursoAsignado(cboCurso.getSelectedItem().toString());
            frmConsultaMatricula.objMatricula.actualizarMatricula();
            frmConsultaMatricula.objMatricula.actualizarMatriculaCurso();
            global.Dialogos.ActualizadoconExito();
            frmConsultaMatricula.generartabla();
            dispose();
        }
    }
    private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarActionPerformed
        if (Valadicaion() == true) {
            set();
            frmConsultaMatricula.objMatricula.actualizarMatricula();
            frmConsultaMatricula.objMatricula.actualizarMatriculaCurso();
            frmConsultaMatricula.generartabla();
            dispose();
            limpiarMatricula();
        }
    }//GEN-LAST:event_btnactualizarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        dispose();
        limpiarMatricula();
    }//GEN-LAST:event_btnsalirActionPerformed

    private void btnbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbuscarActionPerformed
        dlgConsultaAlumno alumno = new dlgConsultaAlumno(null, true);
        alumno.setVisible(true);
    }//GEN-LAST:event_btnbuscarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgAlumno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgAlumno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgAlumno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgAlumno.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dlgMatriculacion dialog = new dlgMatriculacion(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelMatricula;
    private javax.swing.JButton btnactualizar;
    private javax.swing.JButton btnbuscar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton btnsalir;
    public javax.swing.JComboBox cboCurso;
    public javax.swing.JComboBox cbotipomatricula;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JToolBar jToolBar4;
    public static javax.swing.JTextField txtalumnomatricula;
    public static javax.swing.JTextField txtcedulamatricula;
    public static javax.swing.JTextField txtdireccionmatricula;
    public javax.swing.JTextField txtmatricula;
    // End of variables declaration//GEN-END:variables
}
